package com.jsc.controller;

import com.alibaba.fastjson.*;
import com.jsc.XiaMenBankSMUtil;
import com.jsc.common.XiamenUtils;
import com.jsc.common.utils.httputils.HttpSendRequest;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @ClassName BankConrtoller
 * @Date 2021/7/15 0015 下午 14:56
 * @Author Administrator
 * @Description TODO
 * Version 1.0
 */

@Slf4j
@RestController
@RequestMapping("/bank")
public class BankConrtoller {

    @RequestMapping(value = "/getFile")
    @ResponseBody
    public String getGuaranteeInfo(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        multipartRequest.setCharacterEncoding("UTF-8");
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        return fileMap.get("guarantee_file").getOriginalFilename();
    }

    @RequestMapping("/sendFile")
    @ResponseBody
    public String sendFile() throws Exception {
        String json = "{\"name\":\"敦。。。\" }";
        String fileutl = "C:\\Users\\Administrator\\Desktop\\项目修改yml信息.txt";
        String getFile = "http://127.0.0.1.vaiwan.com/xiamebank/bank/getFile";
        Map niu = HttpSendRequest.sendMultiFormMixData(getFile, fileutl, json);
        String sendMess = (String) niu.get("sendMess");
        return sendMess;
    }

    @SneakyThrows
    @RequestMapping(value = "/getMEssag")
    public Map<String, Object> getMEssag(HttpServletRequest request) {
        System.out.println("=========================接收方数据======================");
        System.out.println("data 数据为：" + request.getParameter("data"));
        System.out.println("timeStamp 数据为：" + request.getParameter("timeStamp"));
        System.out.println("nonceStr 数据为：" + request.getParameter("nonceStr"));
        System.out.println("sign 数据为：" + request.getParameter("sign"));
        System.out.println("encrypt 数据为：" + request.getParameter("encrypt"));
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        MultipartFile file = fileMap.get("guarantee_file");
        System.out.println("原文件名为： " + file.getOriginalFilename());

        Map<String, Object> resultData = new HashMap<>();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("serial_num", "保函序列号1111");
        resultMap.put("sign_name", "签约人,即出函机构名称，例如xx银行分行");
        resultMap.put("sign_time", "2018-10-15 11:44:23");
        resultMap.put("issuer", "颁发机构，需中文全称");
        resultMap.put("is_modified", 1);
        resultData.put("status", "200");
        resultData.put("data", resultMap);
        String resultMess = JSON.toJSONString(resultData);
        System.out.println("返回给发送方的原文为：" + resultMess);
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(resultMess, true);
        System.out.println("返回给发送方的报文为：" + JSON.toJSONString(sendPacket));
        System.out.println("=====================================================");

        return sendPacket;
    }

    @SneakyThrows
    @RequestMapping(value = "/main11")
    public Map main11() {

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", "200");

        HashMap<Object, Object> serialmap = new HashMap<>();
        serialmap.put("serial_num", System.currentTimeMillis());
        resultMap.put("data", serialmap);
        /*返回 */
        System.out.println("result"+JSON.toJSONString(resultMap));

        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(JSON.toJSONString(resultMap), true, "timeStamp", "nonceStr");

        System.out.println("返回的json为："+JSON.toJSONString(sendPacket));

        return sendPacket;



//
//        String data = "049b10e0f8c3b941025df8dde1503e6537ec35e817080379c586d609598172519775af4d3e855965cf5909bb65353c41e60088c9cb9b70118398aec51e2c1549b1ac743131a9e23f56e3336dc5bd634289f99e7b3fac7ab127c08fdac9f5cf13078313222f538422ee9ac90fbb75345be16d04b6bbaede016c16870e16d0f2dd65872ab277dddbfb59707fd57a4a0ae6c4855ff9fbda3bbe6c661d641f82aa6a37b615382988d45abf2dced140f73bd7f7284023ec0bce37207c02ff18bcd788d25d4eb1383efac95d767a9b0d6e92468ff026f7f09939bb4e3f59670cb3d987319137d14d40c2f2d7fc9d84b6fbf859e90bad17eca0dcf6e1c4fbab1202227e553a4acd12b83ba2bf6df14d15261f97db371342a51f645faf87e4865f616162db88ea2cbe821a119216cd4e326063a107dfea1c0127d9a6ac28ca8e4bdc9f4b6529f3ff10cd45f84a0a7ab7e1a11dc0669ae650951036585491c48265a4698bff63b0bb830edc04340ea9feeb8397e2b47d8d2b0a74e7d6bc423e7a6165611aea66e875b12a6de684d1175bff7463859fa51ea421f1b022706feec02afc6b582ff960390dca4d659eebd21157e54c81ab8a251b492437ec63d8837c0d7822bbd021060c3f9c3fd0439253c8595fe326bfb2c57418bb3efddb8cede73cb3107dc5e8d16c1b3ec636330434ad9b9f47a87eb05779e90defce";
//        String timeStamp = "1626681165840";
//        String nonceStr = "6ormw9ux";
//        String sign = "MEQCIEsaQzPxYubt42//GVUOFr9yX5+6td9FRzYHZEihlRvlAiBo3WeaJBkiml3BZ3JgYA3ieBi8b62tym0utVEI25W+rA==";
//        String encrypt = "1";
//        Map<String, String> mm = new HashMap<>();
//        mm.put("data", data);
//        mm.put("timeStamp", timeStamp);
//        mm.put("nonceStr", nonceStr);
//        mm.put("sign", sign);
//        mm.put("encrypt", encrypt);
//        String string = JSON.toJSONString(mm);
//        System.out.println(string);
//
//        Map<String, String> stringStringMap = SMUtilTest.verifyAndDecrypt(string, true);
//        String string1 = JSON.toJSONString(stringStringMap);
//        System.out.println("结果为：_"+string1);

    }


    /**
     * 电子保函验真
     * https://xib02.test.xib.com.cn:48080/ifspeesi/gm/api/dlg003/DlgVerification
     */
    @RequestMapping("/sendBank")
    public String sendBank() throws Exception {

        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket("{\"serial_num\":\"bh20210715_01\"}", true);
        String sendMsgPacket = JSON.toJSONString(sendPacket);
        log.info("发送的加密数据为{}", sendMsgPacket);

        String msg = "{\"serial_num\":\"" + UUID.randomUUID().toString() + "\"}";
        String filepath = "D:\\Project\\xiamenguojiyinhang\\sendfile\\牛啊.txt";
        String sendurl = "http://127.0.0.1.vaiwan.com/xiamebank/bank/getbankinfo";
        return XiamenUtils.postFormData(msg, filepath, sendurl);

    }


    @SneakyThrows
    @RequestMapping(value = "/getbankinfo")
    public String dataHandler1(HttpServletRequest request) {
        request.setCharacterEncoding("UTF-8");
        StringBuffer data = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        reader = request.getReader();
        while (null != (line = reader.readLine())) {
            data.append(line);
        }
        String publicMessage = data.toString();
        log.info("报文为:{}" + publicMessage);
        return "接收成功";
    }


    @SneakyThrows
    @RequestMapping(value = "/getElectronicGuaranteeCheck")
    public Map<String, ? extends Object> getElectronicGuaranteeCheck(HttpServletRequest request) {
        HashMap<String, ? extends Object> resultMap = new HashMap<>();
        request.setCharacterEncoding("UTF-8");
        StringBuffer data = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        reader = request.getReader();
        while (null != (line = reader.readLine())) {
            data.append(line);
        }
        String publicMessage = data.toString();
        System.out.println("电子保函查验接收报文为: " + publicMessage);
        Map<String, String> isOK = XiaMenBankSMUtil.verifyAndDecrypt(publicMessage, true);
        if (!isOK.get("code").equals("1")) {
            return isOK;
        }
        String dataInfo = isOK.get("data");
        System.out.println("发送解密后数据为：" + dataInfo);
        System.out.println("。。。。解密成功，正在构造加密返回数据。。。。");
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("guarantee_no", "保函编号_" + UUID.randomUUID().toString());
        returnMap.put("serial_num", "保函序列号_" + UUID.randomUUID().toString());
        returnMap.put("proposer", "申请人");
        returnMap.put("warrantee", "被保证人");
        returnMap.put("beneficiary", "受益人");
        returnMap.put("proitem", "保证事项");
        returnMap.put("variety", 1);
        DecimalFormat df1 = new DecimalFormat("0.00");
        String format = df1.format(200);
        returnMap.put("amount", format);
        returnMap.put("guarantee_start_date", "2018-10-30");
        returnMap.put("guarantee_end_date", "2018-10-30");
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(JSON.toJSONString(returnMap), true);
        return sendPacket;
    }

    @SneakyThrows
    @RequestMapping(value = "/getBidderPaymentPipeline")
    public Map<String, ? extends Object> getBlectronicGuaranteeCheck(HttpServletRequest request) {
        HashMap<String, ? extends Object> resultMap = new HashMap<>();
        request.setCharacterEncoding("UTF-8");
        StringBuffer data = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        reader = request.getReader();
        while (null != (line = reader.readLine())) {
            data.append(line);
        }
        String publicMessage = data.toString();
        System.out.println("电子保函查验接收报文为: " + publicMessage);

        Map<String, String> isOK = XiaMenBankSMUtil.verifyAndDecrypt(publicMessage, false);
        if (!isOK.get("code").equals("1")) {
            return isOK;
        }
        Map map = JSON.parseObject(publicMessage, Map.class);
        String timeStamp = (String) map.get("timeStamp");
        String nonceStr = (String) map.get("nonceStr");


        String dataInfo = isOK.get("data");
        System.out.println("发送解密后数据为：" + dataInfo);
        System.out.println("。。。。解密成功，正在构造加密返回数据。。。。");


        Map<String, Object> result = new HashMap<>();
        Map<String, Object> datas = new HashMap<>();

        List<Object> resultList = new ArrayList<>();
        for (int i = 0; i <= 2; i++) {
            Map<String, Object> listMap = new HashMap<>();
            listMap.put("pay_account_name", "付款账号名称_" + 888);
            listMap.put("pay_account_no", "付款号码" + 888);
            DecimalFormat df1 = new DecimalFormat("0.00");
            String format = df1.format(888 + i);
            listMap.put("pay_amount", format);
            listMap.put("pay_time", "2020-08-12 15:55:22");
            listMap.put("pay_trade_no", "银行交易流水号" + 888);
            listMap.put("note", "附言_" + i);
            resultList.add(listMap);
        }
        datas.put("payment_info_list",  resultList);
        result.put("data",datas);
        result.put("status",1);
        String returnMessage = JSON.toJSONString(result);
        System.out.println(JSON.toJSONString(returnMessage));
        Map<String, Object> sendPacket = XiaMenBankSMUtil.getSendPacket(returnMessage, true,timeStamp,nonceStr);

        return sendPacket;
    }

    public static void main(String[] args) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> datas = new HashMap<>();

        List<Object> resultList = new ArrayList<>();
        for (int i = 1; i <= 2; i++) {
            Map<String, Object> listMap = new HashMap<>();
            listMap.put("pay_account_name", "付款账号名称_" + i);
            listMap.put("pay_account_no", "付款号码" + UUID.randomUUID().toString());
            DecimalFormat df1 = new DecimalFormat("0.00");
            String format = df1.format(200 + i);
            listMap.put("pay_amount", format);
            listMap.put("pay_time", "2020-08-12 15:55:22");
            listMap.put("pay_trade_no", "银行交易流水号" + UUID.randomUUID().toString());
            listMap.put("note", "附言_" + i);
            resultList.add(listMap);
        }
        datas.put("payment_info_list",  resultList);
        result.put("data",datas);
        result.put("status",1);
        String returnMessage = JSON.toJSONString(result);
        System.out.println(JSON.toJSONString(returnMessage));






    }

}
